package com.tutucms.service.admin.controller;

import com.tutucms.common.model.response.ResponseResult;
import com.tutucms.service.admin.model.ro.LoginRo;
import com.tutucms.service.admin.model.vo.UserVo;
import com.tutucms.service.admin.service.AuthService;
import com.tutucms.service.auth.TokenValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @title:
 * @author: wangxudong
 * @date: 2020/10/15
 * @version: 1.0
 * @modified :
 */
@Slf4j
@RestController
@RequestMapping(value = "/auth")
@Api(value = "鉴权接口", tags = "鉴权接口")
public class AuthController {

    @Autowired
    private AuthService authService;

    @ApiOperation(value = "登录")
    @PostMapping("/admin_login")
    public ResponseResult<String> adminLogin(@Validated @RequestBody LoginRo loginRo) {

        return ResponseResult.successResult(authService.login(loginRo));
    }

    @ApiOperation(value = "验证码图片地址")
    @GetMapping(value = "/pic_captcha", produces = MediaType.IMAGE_JPEG_VALUE)
    public void getCaptcha(HttpServletResponse response) throws IOException {

        authService.captchaImage(response);
    }

    @TokenValidator
    @ApiOperation(value = "获取当前登录用户")
    @GetMapping("/current_admin_user")
    public ResponseResult<UserVo> currentUser() {

        return ResponseResult.successResult(authService.getCurrentUser());
    }

}
